package com.frinder.official.spring;

import akka.actor.AbstractLoggingActor;
import akka.actor.OneForOneStrategy;
import akka.actor.SupervisorStrategy;
import akka.event.LoggingAdapter;
import akka.japi.pf.DeciderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import scala.concurrent.duration.Duration;

import java.util.concurrent.TimeUnit;

import static akka.actor.SupervisorStrategy.*;

/**
 * Created by frinder on 2017/9/13.
 * <p>
 * 注：再次封装，加入统一日志及工具注入类
 */
public abstract class MyBaseActor extends AbstractLoggingActor {

    protected final LoggingAdapter log = log();

    @Autowired
    protected Actors actors;

    @Autowired
    protected SpringExtension springExtension;

    private static SupervisorStrategy strategy =
            new OneForOneStrategy(10,
                    Duration.create(1, TimeUnit.MINUTES),
                    DeciderBuilder
                            .match(ArithmeticException.class, e -> resume())
                            .match(NullPointerException.class, e -> restart())
                            .match(IllegalArgumentException.class, e -> stop())
                            .matchAny(o -> escalate()).build()
            );

    @Override
    public SupervisorStrategy supervisorStrategy() {
        return strategy;
    }

}
